/*
 * @Description:
 * @Author: 胡涛
 * @Date: 2023-09-26 09:04:13
 * @LastEditors: 胡涛
 * @LastEditTime: 2024-07-18 22:53:10
 */
/*
 * @Description:
 * @Author: 胡涛
 * @Date: 2023-09-11 09:21:37
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-11-12 00:52:49
 */
import { SystemMenuDto } from "@/api/system/types";
import { localMenus } from "@/config/menu";
import { useLocalMenu } from "@/environment";
import { useAppStore } from "@/store";
import { ISideMenuItem } from "@/types/menu";
import { ref } from "vue";
import { useRoute } from "vue-router";
import { EmptyGuid } from "./contants";
import { getIconComponent } from "./icons";

const currentSystemMenu = ref<Array<ISideMenuItem>>([]);
/**
 * 创建系统菜单
 * @param groupName
 * @returns
 */
export const createSystemMenu = (groupName: string) => {
  if (useLocalMenu) {
    currentSystemMenu.value = localMenus;
    return localMenus;
  }
  const appStore = useAppStore();
  let allPerms = Object.keys(appStore.grantedPolicies);
  let grantedPolicies = allPerms.filter((k) => appStore.grantedPolicies[k]);
  let allMenus = appStore.allMenus.filter(
    (x) =>
      x.groupName == groupName &&
      (!x.permissionName ||
        grantedPolicies.findIndex(
          (p) => p == x.permissionName || p.startsWith(x.permissionName + ".")
        ) != -1)
  );
  let items = createChildrenMenu(allMenus);
  items = items.filter((x) => x.url || x.children?.length);
  currentSystemMenu.value = items;
  return currentSystemMenu.value;
};

/**
 * 创建下级菜单
 * @param allMenus
 * @param parentId
 * @returns
 */
const createChildrenMenu = (
  allMenus: Array<Partial<SystemMenuDto>>,
  parentId?: string
): Array<ISideMenuItem> => {
  let items: Array<ISideMenuItem> = [];
  let callback = (x: SystemMenuDto) =>
    (!parentId && (!x.parentId || x.parentId == EmptyGuid)) ||
    (parentId && x.parentId == parentId);
  items = allMenus.filter(callback).map((x) => {
    let menu: ISideMenuItem = {
      key: x.id,
      label: x.displayName,
      url: x.url,
    };
    if (x.icon) {
      // console.log("x", x, x.icon);
      menu.icon = getIconComponent(x.icon);
    } else {
      menu.icon = getIconComponent("desktop");
    }
    let children = createChildrenMenu(allMenus, x.id);
    if (children.length) {
      menu.children = children;
    }
    return menu;
  });
  return items;
};

/**
 * 根据键名获取对应的菜单项URL
 * @param key
 * @param items
 * @returns
 */
export const getMenuUrlForKey = (key: string, items?: Array<ISideMenuItem>) => {
  if (!items) {
    items = currentSystemMenu.value;
  }
  let item = items.find((x) => x.key == key);
  if (item) {
    return item.url;
  }
  for (let i in items) {
    if (items[i].children) {
      let url = getMenuUrlForKey(key, items[i].children);
      if (url) {
        return url;
      }
    }
  }
  return null;
};

/**
 * 根据路由查找菜单项
 */
export const getMenuKeysForRoute = (path): Array<string> => {
  let items = findMenuItemsForUrl(path, currentSystemMenu.value);
  return items?.map((x) => x.key) || [];
};

const findMenuItemsForUrl = (path: string, items: Array<ISideMenuItem>) => {
  let item = items.find((x) => path.startsWith(x.url));
  if (item) {
    return [item];
  }
  for (let i in items) {
    if (items[i].children) {
      let sub = findMenuItemsForUrl(path, items[i].children);
      if (sub) {
        return [items[i], ...sub];
      }
    }
  }
  return null;
};
